/*
 * @Author: ipk518 18163976442@163.com
 * @Date: 2023-08-16 16:16:35
 * @LastEditors: ipk518 18163976442@163.com
 * @LastEditTime: 2023-08-19 15:17:53
 * @FilePath: /WingsUpdater-main/src/Misc/Description.cpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include "Description.h"
#include <experimental/filesystem>
#include "myLog.h"
#include "Ini.h"

namespace Description 
{

using namespace std;

describeItem *currentDescribe() 
{
    static describeItem singleton;

    return &singleton;
}

Result loadDescription(const std::string &path)
{
    int res = 0;
    char buffer[100];
    unsigned char tmp;
    if (false == experimental::filesystem::is_regular_file(path)) 
    {
        auto err = Result::create(ErrorKind::FileNotExist);
        auto msg = string("file path: '") + path + "'";
        err.appendInfo(msg);

        return err;
    }
    auto confPath = path.c_str();
    auto conf = currentDescribe();
    auto fileName = "the Description.ini file' is empty";
    memset(buffer,'\0',sizeof(buffer));
    res = readStringValue((char *)"Name",(char *)"updatePack",buffer,confPath);
    
    tmp = (unsigned char)(strchr(buffer, '\n') - buffer);
    conf->m_PackName = (tmp > 0)?strndup(buffer, tmp):strdup(buffer);
    if (res != 0 || true == conf->m_PackName.empty()) 
    {
        auto err = Result::create(ErrorKind::InvalidData);
        auto msg = string("The updatePack in the Name title of '") + fileName;
        err.appendInfo(msg);

        return err;
    }

    memset(buffer,'\0',sizeof(buffer));
    res = readStringValue((char *)"Link",(char *)"uniqueSymbolLink",buffer,confPath);

    tmp = (unsigned char)(strchr(buffer, '\n') - buffer);
    conf->m_link.uniqueSymbolLink = (tmp > 0)?strndup(buffer, tmp):strdup(buffer);
    if (res != 0 || true == conf->m_link.uniqueSymbolLink.empty()) 
    {
        auto err = Result::create(ErrorKind::InvalidData);
        auto msg = string("The uniqueSymbolLink in the Link title of '") + fileName;
        err.appendInfo(msg);

        return err;
    }

    memset(buffer,'\0',sizeof(buffer));
    res = readStringValue((char *)"Link",(char *)"pingBank",buffer,confPath);

    tmp = (unsigned char)(strchr(buffer, '\n') - buffer);
    conf->m_link.pingBank = (tmp > 0)?strndup(buffer, tmp):strdup(buffer);
    if (res != 0 || true == conf->m_link.pingBank.empty()) 
    {
        auto err = Result::create(ErrorKind::InvalidData);
        auto msg = string("The pingBank in the Link title of '") + fileName;
        err.appendInfo(msg);

        return err;
    }

    memset(buffer,'\0',sizeof(buffer));
    res = readStringValue((char *)"Link",(char *)"pongBank",buffer,confPath);

    tmp = (unsigned char)(strchr(buffer, '\n') - buffer);
    conf->m_link.pongBank = (tmp > 0)?strndup(buffer, tmp):strdup(buffer);
    if (res != 0 || true == conf->m_link.pongBank.empty()) 
    {
        auto err = Result::create(ErrorKind::InvalidData);
        auto msg = string("The pongBank in the Link title of '") + fileName;
        err.appendInfo(msg);

        return err;
    }

    memset(buffer,'\0',sizeof(buffer));
    res = readStringValue((char *)"application",(char *)"src",buffer,confPath);

    tmp = (unsigned char)(strchr(buffer, '\n') - buffer);
    conf->m_app.src = (tmp > 0)?strndup(buffer, tmp):strdup(buffer);
    if (res != 0 || true == conf->m_app.src.empty()) 
    {
        auto err = Result::create(ErrorKind::InvalidData);
        auto msg = string("The src in the application title of '") + fileName;
        err.appendInfo(msg);

        return err;
    }

    memset(buffer,'\0',sizeof(buffer));
    conf->m_app.directory = readIntValue((char *)"application",(char *)"directory",confPath);
    if (conf->m_app.directory < 0) 
    {
        auto err = Result::create(ErrorKind::InvalidData);
        auto msg = string("The directory in the application title of '") + fileName;
        err.appendInfo(msg);

        return err;
    }

    memset(buffer,'\0',sizeof(buffer));
    res = readStringValue((char *)"image",(char *)"src",buffer,confPath);

    tmp = (unsigned char)(strchr(buffer, '\n') - buffer);
    conf->m_image.src = (tmp > 0)?strndup(buffer, tmp):strdup(buffer);
    if (res != 0 || true == conf->m_image.src.empty()) 
    {
        auto err = Result::create(ErrorKind::InvalidData);
        auto msg = string("The src in the image title of '") + fileName;
        err.appendInfo(msg);

        return err;
    }

    memset(buffer,'\0',sizeof(buffer));
    res = readStringValue((char *)"image",(char *)"bootPartition",buffer,confPath);

    tmp = (unsigned char)(strchr(buffer, '\n') - buffer);
    conf->m_image.bootPartition = (tmp > 0)?strndup(buffer, tmp):strdup(buffer);
    if (res != 0 || true == conf->m_image.bootPartition.empty()) 
    {
        auto err = Result::create(ErrorKind::InvalidData);
        auto msg = string("The bootPartition in the image title of '") + fileName;
        err.appendInfo(msg);

        return err;
    }

    memset(buffer,'\0',sizeof(buffer));
    res = readStringValue((char *)"image",(char *)"mountPoint",buffer,confPath);

    tmp = (unsigned char)(strchr(buffer, '\n') - buffer);
    conf->m_image.mountPoint = (tmp > 0)?strndup(buffer, tmp):strdup(buffer);
    if (res != 0 || true == conf->m_image.mountPoint.empty()) 
    {
        auto err = Result::create(ErrorKind::InvalidData);
        auto msg = string("The mountPoint in the image title of '") + fileName;
        err.appendInfo(msg);

        return err;
    }

    memset(buffer,'\0',sizeof(buffer));
    conf->m_image.directory = readIntValue((char *)"image",(char *)"directory",confPath);
    if (conf->m_image.directory < 0) 
    {
        auto err = Result::create(ErrorKind::InvalidData);
        auto msg = string("The directory in the image title of '") + fileName;
        err.appendInfo(msg);

        return err;
    }

    memset(buffer,'\0',sizeof(buffer));
    res = readStringValue((char *)"driver",(char *)"src",buffer,confPath);

    tmp = (unsigned char)(strchr(buffer, '\n') - buffer);
    conf->m_driver.src = (tmp > 0)?strndup(buffer, tmp):strdup(buffer);
    if (res != 0 || true == conf->m_driver.src.empty()) 
    {
        auto err = Result::create(ErrorKind::InvalidData);
        auto msg = string("The src in the driver title of '") + fileName;
        err.appendInfo(msg);

        return err;
    }

    memset(buffer,'\0',sizeof(buffer));
    res = readStringValue((char *)"driver",(char *)"dst",buffer,confPath);

    tmp = (unsigned char)(strchr(buffer, '\n') - buffer);
    conf->m_driver.dst = (tmp > 0)?strndup(buffer, tmp):strdup(buffer);
    if (res != 0 || true == conf->m_driver.dst.empty()) 
    {
        auto err = Result::create(ErrorKind::InvalidData);
        auto msg = string("The dst in the driver title of '") + fileName;
        err.appendInfo(msg);

        return err;
    }

    memset(buffer,'\0',sizeof(buffer));
    conf->m_driver.directory = readIntValue((char *)"driver",(char *)"directory",confPath);
    if (conf->m_driver.directory < 0) 
    {
        auto err = Result::create(ErrorKind::InvalidData);
        auto msg = string("The directory in the driver title of '") + fileName;
        err.appendInfo(msg);

        return err;
    }

    memset(buffer,'\0',sizeof(buffer));
    res = readStringValue((char *)"other",(char *)"src",buffer,confPath);

    tmp = (unsigned char)(strchr(buffer, '\n') - buffer);
    conf->m_other.src = (tmp > 0)?strndup(buffer, tmp):strdup(buffer);
    if (res != 0 || true == conf->m_other.src.empty()) 
    {
        auto err = Result::create(ErrorKind::InvalidData);
        auto msg = string("The src in the other title of '") + fileName;
        err.appendInfo(msg);

        return err;
    }

    memset(buffer,'\0',sizeof(buffer));
    res = readStringValue((char *)"other",(char *)"dst",buffer,confPath);

    tmp = (unsigned char)(strchr(buffer, '\n') - buffer);
    conf->m_other.dst = (tmp > 0)?strndup(buffer, tmp):strdup(buffer);
    if (res != 0 || true == conf->m_other.dst.empty()) 
    {
        auto err = Result::create(ErrorKind::InvalidData);
        auto msg = string("The dst in the other title of '") + fileName;
        err.appendInfo(msg);

        return err;
    }

    memset(buffer,'\0',sizeof(buffer));
    conf->m_other.directory = readIntValue((char *)"other",(char *)"directory",confPath);
    if (conf->m_other.directory < 0) 
    {
        auto err = Result::create(ErrorKind::InvalidData);
        auto msg = string("The directory in the driver title of '") + fileName;
        err.appendInfo(msg);

        return err;
    }

    return Result();
}

} // end anonymous namespace